#include <htc.h>

int key = -1;
int old_key = -1;
int reset = 0;
int old_RB0 = 1;
int open = 0;
int old_RE0 = 1;

void main()
{

	ANSEL=0;
	ANSELH=0;
	TRISB0=1;	// Set RB0: reset as input
	TRISE0=1;	// Set RE0: open as input
	TRISE1=0;	// Set RE1: lock as output
	RE1=1;		// Set lock to close
	TRISE2=0;	// Set RE2: buzzer as output

	TRISB4=0;	// Set RB4: valid as output
	RB4=0;
	TRISB5=0;	// Set RB5: invalid as output
	RB5=0;
	

	for(int j=0; j<255; j++)
		eeprom_write(j, 0xFF);
	eeprom_write(0,'1');
	eeprom_write(1,'2');
	eeprom_write(2,'3');
	eeprom_write(3,'4');
	eeprom_write(4,'5');
	eeprom_write(5,'6');

	// Read numpad

	TRISD5=0; TRISD6=0; TRISD7=0;
	TRISD4=1; TRISC5=1; TRISC6=1; TRISC7=1;
	
	long i=0;
	bool valid;
	valid=1;
	for(;;){

		if( old_RE0==1 && RE0==0 ) {
			open = 1;
		}
		old_RE0 = RE0;

		if(open==1) {
			RE1 = 0;
			RB4 = 1;
			for(int j=0;j<300;j++)
				for(int k=0;k<600; k++)
					__delay_ms(1000);
			open=0;
			RE1 = 1;
			RB4 = 0;
		}

		if( old_RB0==1 && RB0==0 ) {
			reset = 1-reset;
			RB5 = 1;
			for(int j=0; j<255; j++)
				eeprom_write(j, 0xFF);
			RE1 = 0;
			i=0;
		}
		old_RB0 = RB0;

		old_key=key;
		scan();
		char c = '0'+key;

		/*
		| *******************
		| Reset password mode
		| *******************
		*/

		if(reset==1) { 

		if(old_key==-1 && key!=-1) {

			beep();

			if((c=='<'||i>14) && (i>5)) // in case user done enter new password
			{
				reset = 0;
				RB5 = 0;
				i=0;
			}
			else if(c==':') // in case user want to enter again
			{
				for(int j=0; j<255; j++)
					eeprom_write(j, 0xFF);
				i=0;
			}
			else if(c!='<') { // in case of any character but enter 
				eeprom_write(i, c);
				i++;
			}

		} // end check key

		}	

		/*
		| *******************
		| Enter password mode
		| *******************
		*/

		else {

		if(old_key==-1 && key!=-1) {

			beep();

			if(c!='<'&&i<=14&&eeprom_read(i)!=c) valid=0;

			if(c=='<'||i>15)
			{
				if(i>15) valid=0;
				else if(eeprom_read(i)!=255) valid=0;

				i=0;
				if(valid) {
					//uart_puts("Welcome");
					open=1;
					RB5=0;
				}
				else {
					//uart_puts("Wrong");
					open=0;
					RB5=1;
					valid=1;
				}
			}
			else 
			if(c!=':') { // Any correct number and not the clear button
				i++;
			}
			else {
				i=0; valid=1;
				RB5=0;
			}
		} // end check key
		
		} // end enter password

		

	} // end infinite loop
	
}

void scan() {

		key=-1;
		
		RD7=0; RD6=1; RD5=1;
		if(RD4==0) key=12;
		if(RC7==0) key=9;
		if(RC6==0) key=6;
		if(RC5==0) key=3;

		RD7=1; RD6=0; RD5=1;
		if(RD4==0) key=0;
		if(RC7==0) key=8;
		if(RC6==0) key=5;
		if(RC5==0) key=2;

		RD7=1; RD6=1; RD5=0;
		if(RD4==0) key=10;
		if(RC7==0) key=7;
		if(RC6==0) key=4;
		if(RC5==0) key=1;
}

void beep() {
	RE2 = 1;
	for(int j=0;j<1000; j++) {
			RE2 = 1-RE2;
			__delay_ms(100);	
		}
	RE2 = 0;
}